﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils.Common;
using ZK.BLL;

namespace ZK.Client.WinForm
{
    public partial class FrmCustomer : Form
    {
        public FrmCustomer()
        {
            InitializeComponent();
        }

        ImageList imageList = new ImageList();
        private void FrmCustomer_Load(object sender, EventArgs e)
        {

            LoadImages();
        }

        private void LoadImages()
        {
            this.lvwData.Items.Clear();
            imageList.Images.Clear();
            imageList.ImageSize = new Size(45,15);
            string[] files = Directory.GetFiles(ConfigBase.LogoImg);
            foreach (var file in files)
            {
                AddFileToListView(file);
            }

            this.lvwData.SmallImageList = imageList;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = "图片|*.bmp";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {

                string filePath = openFileDialog.FileName;
                string fileName = Path.GetFileName(filePath);
                string destFile = ConfigBase.LogoImg + "\\" + fileName;

                if (File.Exists(destFile))
                {
                    if (MessageBox.Show("文件已存在是否覆盖", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                    {
                        return;
                    }
                }
                try
                {

                    using (Image img = Image.FromFile(filePath))
                    {
                        if (Class_Config.PrinetType == "WDJ" || Class_Config.PrinetType == "VDJ")
                        {

                            if (!(img.Height == 16 || img.Height == 24 || img.Height == 32))
                            {
                                MessageBox.Show("图像高度必须是16、24、32像素！");
                                return;
                            }

                            if (img.Height == 16 && img.Width > 230)
                            {
                                MessageBox.Show("16像素高度的图像宽度应小于等于230像素！");
                                return;
                            }

                            if (img.Height == 24 && img.Width > 160)
                            {
                                MessageBox.Show("24像素高度的图像宽度应小于等于160像素！");
                                return;
                            }

                            if (img.Height == 32 && img.Width > 116)
                            {
                                MessageBox.Show("32像素高度的图像宽度应小于等于 116像素！");
                                return;
                            }
                        }
                    }
                    File.Copy(filePath, destFile, true);
                    AddFileToListView(destFile);

                    LoadImages();
                }
                catch (Exception ex){
                    Console.WriteLine(ex.Message);
                }
            }
        }




        /// <summary>
        /// 将文件添加到ListView中显示
        /// </summary>
        /// <param name="fileInfo">文件全路径</param>
        /// <param name="keywords">搜索关键字</param>
        private void AddFileToListView(string fileName)
        {
            string key = Path.GetFileNameWithoutExtension(fileName);
            if (!imageList.Images.ContainsKey(fileName))
            {
                using (Image img = Image.FromFile(fileName))
                {

            
                    Bitmap imgNew = new Bitmap(img);
                    imageList.Images.Add(key, imgNew);
                }
            }
          
            var listViewItem= this.lvwData.Items.Add("   "+key);
            listViewItem.ImageIndex = imageList.Images.IndexOfKey(key);
            
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (this.lvwData.SelectedItems.Count > 0)
            {

                var listViewItem = this.lvwData.SelectedItems[0];

                string val = listViewItem.Text.Trim();
                if (MessageBox.Show($"确定要删除【{val}】", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    string file = ConfigBase.LogoImg + "\\"+ val+".bmp";
                    if (File.Exists(file))
                    {
                        File.Delete(file);
                    }

                    LoadImages();
                }

            }
        }

        private void btnReName_Click(object sender, EventArgs e)
        {
            if (this.lvwData.SelectedItems.Count > 0)
            {

                try
                {

                    var listViewItem = this.lvwData.SelectedItems[0];

                    string val = listViewItem.Text.Trim();
                    FrmReName frm = new FrmReName();
                    frm.ReName = val;
                    if (frm.ShowDialog() == DialogResult.OK)
                    {

                        string fileName = ConfigBase.LogoImg + "\\" + val + ".bmp";
                        string newName = ConfigBase.LogoImg + "\\" + frm.ReName + ".bmp";
                        if (File.Exists(fileName))
                        {
                            FileInfo fi = new FileInfo(fileName);
                            fi.MoveTo(newName);
                        }
                        LoadImages();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
